package com.iqiyi.pps.epg.web.webapp.action.stream;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.pps.epg.api.model.web.stream.TChannelStream;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.model.channel.ChannelConfig;
import com.iqiyi.pps.epg.core.service.channel.ChannelService;
import com.iqiyi.pps.epg.core.utils.Constants;
import com.iqiyi.pps.epg.core.utils.ApiHttpRequestUtil;
import com.iqiyi.pps.epg.web.utils.InnerApiUtil;
import com.iqiyi.pps.epg.web.utils.Struts2Utils;
import com.iqiyi.pps.epg.web.webapp.action.BaseAction;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2014/12/15.
 */
@Namespace("/stream/stream")
@Scope("prototype")
public class StreamAction extends BaseAction<TChannelStream> {

    private Page<TChannelStream> page;

    @Override
    public TChannelStream getModel() {
        return entity;
    }

    private Long id;

    private TChannelStream entity;

    private List<ChannelConfig> channelConfigs;

    @Autowired
    private ChannelService channelService;

    @Override
    @Action(value = "list", results = {@Result(name = "success", location = "/WEB-INF/content/stream/stream/list.jsp")})
    public String list() throws Exception {
        String url = String.format(InnerApiUtil.BASE_URL, InnerApiUtil.STREAM_LIST);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("fatherId", Struts2Utils.getParameter("fatherId"));
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put("Sign", InnerApiUtil.TEST_SIGN);
        JSONObject jsonObject = ApiHttpRequestUtil.postMethod(url, headers, params);
        List<TChannelStream> tChannelStreams = new ArrayList<TChannelStream>();
        if (jsonObject != null && jsonObject.getString("code").equals(Constants.CODE_SUC)) {
            String data = jsonObject.getString("data");
            tChannelStreams = JSON.parseArray(data, TChannelStream.class);
            channelConfigs = channelService.getConfig("StreamType");
            if (channelConfigs != null && channelConfigs.size() > 0) {
                for (TChannelStream tChannelStream : tChannelStreams) {
                    for (ChannelConfig channelConfig : channelConfigs) {
                        if (tChannelStream.getStreamType().equals(channelConfig.getVal())) {
                            tChannelStream.setStreamType(channelConfig.getDescr());
                        }
                    }
                }
            }
        }

        if (!page.isOrderBySetted()) {
            page.setOrderBy("id");
            page.setOrder(Page.ASC);
        }
        page.setPageSize(10);
        page.setPageNo(1);
        page.setTotalCount(tChannelStreams.size());
        page.setResult(tChannelStreams);
        return SUCCESS;
    }

    @Override
    protected void prepareModel() throws Exception {
        System.out.println("[******StreamAction******][prepareModel被调用]");

    }

    @Override
    @Action(value = "input")
    public String input() throws Exception {
        channelConfigs = channelService.getConfig("StreamType");
        if (id != null) {
            String url = String.format(InnerApiUtil.BASE_URL, InnerApiUtil.STREAM_GET);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("id", id + "");
            Map<String, Object> headers = new HashMap<String, Object>();
            headers.put("Sign", InnerApiUtil.TEST_SIGN);
            JSONObject jsonObject = ApiHttpRequestUtil.postMethod(url, headers, params);
            if (jsonObject != null && jsonObject.getString("code").equals(Constants.CODE_SUC)) {
                String data = jsonObject.getString("data");
                entity = JSON.parseObject(data, TChannelStream.class);
            }
        }
        return SUCCESS;
    }

    @Override
    @Action(value = "save")
    public String save() throws Exception {
        channelConfigs = channelService.getConfig("StreamType");
        String url = null;
        Map<String, Object> params = new HashMap<String, Object>();
        if (entity.getId() == 0) {
            url = String.format(InnerApiUtil.BASE_URL, InnerApiUtil.STREAM_CREATE);
            params.put("fatherId", entity.getFatherId() + "");
            params.put("streamName", entity.getStreamName());
            params.put("streamIp", entity.getStreamIp());
            params.put("streamType", entity.getStreamType());
            params.put("peerCount", entity.getPeerCount() + "");
        } else {
            url = String.format(InnerApiUtil.BASE_URL, InnerApiUtil.STREAM_EDIT);
            params.put("id", entity.getId() + "");
            params.put("streamIp", entity.getStreamIp());
            params.put("streamType", entity.getStreamType());
            params.put("peerCount", entity.getPeerCount() + "");
            params.put("isEnable", entity.getIsEnable());
        }
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put("Sign", InnerApiUtil.TEST_SIGN);
        String msg = ApiHttpRequestUtil.forceGetResponseMsg(url, headers, params);
        addActionMessage(msg);
        return INPUT;
    }

    @Override
    @Action(value = "delete")
    public String delete() throws Exception {
        String url = String.format(InnerApiUtil.BASE_URL, InnerApiUtil.STREAM_DELETE_BYID);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id + "");
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put("Sign", InnerApiUtil.TEST_SIGN);
        JSONObject jsonObject = ApiHttpRequestUtil.postMethod(url, headers, params);
        renderAjaxMessage(jsonObject.getString("code"), jsonObject.getString("msg"));
        return null;
    }

    public Page<TChannelStream> getPage() {
        return page;
    }

    public void setPage(Page<TChannelStream> page) {
        this.page = page;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List<ChannelConfig> getChannelConfigs() {
        return channelConfigs;
    }

    public void setChannelConfigs(List<ChannelConfig> channelConfigs) {
        this.channelConfigs = channelConfigs;
    }

    public TChannelStream getEntity() {
        return entity;
    }

    public void setEntity(TChannelStream entity) {
        this.entity = entity;
    }

}
